﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_DTTN.DTO.DTO;
using QL_DTTN.KQT.BUS;
using QL_DTTN.DTO;
using System.Linq;
namespace QL_DTTN.KQT
{
    public partial class FrmImportKetQuaThi : DevExpress.XtraEditors.XtraForm
    {
        DataTable _dtKetQua;
        private TaiKhoan _taiKhoan;

        public TaiKhoan TaiKhoan
        {
            set { _taiKhoan = value; }
        }
        public FrmImportKetQuaThi()
        {
            InitializeComponent();
            _taiKhoan = new DTO.DTO.TaiKhoan();
            _dtKetQua = new DataTable();
            _dtKetQua.Columns.Add("SoBaoDanh");
            _dtKetQua.Columns.Add("HoTen");
            _dtKetQua.Columns.Add("KetQua");
        }
        private void FrmImportKetQuaThi_Load(object sender, EventArgs e)
        {
            cbxKyThi_Load();
            cbxKyThi_SelectedIndexChanged(sender, e);
            gctKetQua.DataSource = _dtKetQua;
        }
        private void btnChonFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofile = new OpenFileDialog();
            txtDuongDan.Text = ofile.ShowDialog() == DialogResult.OK ? ofile.FileName : "";
            if (txtDuongDan.Text == "")
            {
                return;
            }
            loadGrid();
        }
        private void loadGrid()
        {
            DataTable dt = ExcelDataProvider.ReadDataFromExcelFile(txtDuongDan.Text.Trim());
            _dtKetQua.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = _dtKetQua.NewRow();
                dr["SoBaoDanh"] = dt.Rows[i][0].ToString();
                dr["HoTen"] = dt.Rows[i][1].ToString();
                dr["KetQua"] = dt.Rows[i][2].ToString();
                _dtKetQua.Rows.Add(dr);
            }
            gvwKetQua.RefreshData();
        }
        private void cbxKyThi_Load()
        {
            cbxKyThi.DataSource = KyThi_Bus.dataTable();
            cbxKyThi.ValueMember = "maKyThi";
            cbxKyThi.DisplayMember = "tenKyThi";
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            List<KetQuaThi> lstThem = new List<KetQuaThi>();
            List<KetQuaThi> lstSua = new List<KetQuaThi>();
            foreach (DataRow dr in _dtKetQua.Rows)
            {
                KetQuaThi kq = new KetQuaThi();
                kq.SoBaoDanh = dr["SoBaoDanh"].ToString();
                kq.MaMon = Convert.ToInt16(cbxMon.SelectedValue);
                kq.KetQua = Convert.ToDouble(dr["KetQua"]);
                kq.TenTaiKhoan = _taiKhoan.TenTaiKhoan;
                if (!(lstSua.Where(t => t.SoBaoDanh == kq.SoBaoDanh && t.MaMon == kq.MaMon).Count() > 0 || lstThem.Where(t => t.SoBaoDanh == kq.SoBaoDanh && t.MaMon == kq.MaMon).Count() > 0))
                {
                    if (KetQuaThi_Bus.KiemTraTonTai(kq))
                    {
                        lstSua.Add(kq);
                    }
                    else lstThem.Add(kq);
                }
            }
            if (!KetQuaThi_Bus.Them(lstThem))
            { XtraMessageBox.Show("Thêm thất bại"); return; }
            if (!KetQuaThi_Bus.Sua(lstSua))
            { XtraMessageBox.Show("Cập nhật thất bại"); return; }
            XtraMessageBox.Show("Cập nhật từ file thành công");
            _dtKetQua.Rows.Clear();
            gvwKetQua.RefreshData();
            txtDuongDan.Text = "";

        }

        private void cbxKyThi_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                long makt = 0;
                if (long.TryParse(cbxKyThi.SelectedValue.ToString(), out makt))
                {
                    cbxMon.DataSource = MonThi_Bus.dataTableMon(makt);
                    cbxMon.ValueMember = "MaMon";
                    cbxMon.DisplayMember = "TenMon";
                }
            }
            catch
            {

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult = MessageBox.Show("Bạn có chắc muốn thoát không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (DialogResult == DialogResult.OK)
            {
                this.Close();
            }
        }

       
    }
}